package com.example.springboot.service.impl;;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.entity.Book;
import com.example.springboot.entity.Category;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.BookMapper;
import com.example.springboot.mapper.CategoryMapper;
import com.example.springboot.service.ICategoryService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService{

    @Autowired
    private BookMapper bookMapper;

    @Override
    public boolean save(Category entity) {
        return super.save(entity);
    }

    @Override
    public boolean updateById(Category entity) {
        return super.updateById(entity);
    }

    @Override
    public boolean removeById(Category entity) {
        return super.removeById(entity);
    }

    @Override
    public boolean removeBatchByIds(Collection<?> list) {
        return super.removeBatchByIds(list);
    }

    @Override
    public List<Category> list() {
        return super.list();
    }

    @Override
    public Category getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public <E extends IPage<Category>> E page(E page, Wrapper<Category> queryWrapper) {
        return super.page(page, queryWrapper);
    }

    @Override
    public List<Category> bookCount() {
        List<Category> categories = super.list();
        categories.stream().forEach(category -> {
            category.setCount(bookMapper.selectCount(new LambdaQueryWrapper<Book>().eq(Book::getCategoryId,category.getId())));
        });
        categories.sort((c1, c2) -> Long.compare(c2.getCount(), c1.getCount()));
        return categories;
    }

}
